<?php
/**
 * 提货记录/日志
 * User: bgl
 * Date: 2023/2/2
 * Time: 18:23
 */

namespace App\Admin\Controllers\ProductPickup;


use App\Models\Product\ProductClassify;
use App\Models\ProductPickup\PickupOrder;
use App\Models\ProductPickup\PickupOrderLog;
use App\Util\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Grid;
use Encore\Admin\Widgets\Table;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Request;

class PickupOrderLogController extends AdminController{

    protected $title = '提货记录（新）';

    protected function grid(){

        $grid = new Grid(new PickupOrderLog());
        $grid->model()->groupBy('pickup_no');
        $grid->column('pickup_no','提货单号')->expand(function ($model) use($grid){

            $skuList = PickupOrderLog::query()
                ->where('pickup_no', $model->pickup_no)
                ->orderBy('cycle','DESC')
                ->groupBy('cycle')
                ->get();
            return view('product-pickup.pickup-order-log.index', compact('skuList'));
        });

        /*$grid->column('cycle', '期数')->display(function (){
            return PickupOrderLog::query()->where('pickup_no',$this->pickup_no)->max('cycle');
        });
        $grid->column('sku.psku_code', 'SKU')->display(function ($value){
            return '';
        });
        $grid->column('month', '月份')->display(function ($value){
            return '';
        });
        $grid->column('seller_id', '提货员')->display(function ($value){
            return '';
        });*/

        $grid->disableCreateButton();
        $grid->disableRowSelector();
        $grid->disableActions();
        $grid->filter(function($filter){
            $filter->column(1/3, function ($filter) {
                $filter->where(function ($query) {
                    $query->whereHas('sku', function ($query) {
                        $query->where('psku_code', 'like', "%{$this->input}%")
                            ->orWhere('psku_cn', 'like', "%{$this->input}%");
                    });
                }, 'SKU','sku8')->placeholder('模糊查询SKU，名称');
            });
            $filter->column(1/3, function ($filter){
                $filter->equal('skuClassify.season', '季节性')->select(ProductClassify::$_season);
                $filter->equal('sku.classify_id', '品类')->select(ProductClassify::options());
            });
        });
        return $grid;
    }

    public function download(){

        echo "<pre>";
        print_r(111);
        echo "</pre>";
        exit;

        $request = Request::input();
        $query = $this->_initQuery($request);

        //PickupOrder::exeDownload($query);
    }

    private function _initQuery($filters){

        $query = PickupOrder::query()->orderBy('id', 'DESC');

        foreach ($filters as $key => $_filter){
            if($key == 'sku8'){
                $query->whereHas('sku', function ($query) use($_filter){
                    $query->where('psku_code', 'like', "%{$_filter}%")
                        ->orWhere('psku_cn', 'like', "%{$_filter}%");
                });
            }
            if($key == 'sku'){
                $query->whereHas('sku', function ($query) use($_filter){
                    $query->where('classify_id', $_filter['classify_id']);
                });
            }
            if($key == 'skuClassify'){
                $query->whereHas('skuClassify', function ($query) use($_filter){
                    $query->where('season', $_filter['season']);
                });
            }
            if(in_array($key,['area_id','status'])){
                $query->where($key,$_filter);
            }
        }
        return $query;
    }
}
